//
//  AyStatusTool.m
//  xWeibo
//
//  Created by alvis mac on 10/31/15.
//  Copyright © 2015 alvis. All rights reserved.
//

#import "AyStatusTool.h"
#import "AyHttpTool.h"
#import "MJExtension.h"
#import "AyStatusCacheTool.h"

@implementation AyStatusTool

+ (void)homeStatusesWithParam:(AyHomeStatusesParame *)param success:(void (^)(AyHomeStatusesResult *))success failure:(void (^)(NSError *))failure{
    
    NSArray *statusArray=[AyStatusCacheTool statuesWithParam:param];
    if (statusArray.count) {
        if (success) {
            AyHomeStatusesResult *result=[[AyHomeStatusesResult alloc] init];
            result.statuses=statusArray;
            success(result);
        }
    }else{
        [AyHttpTool getWithURL:@"https://api.weibo.com/2/statuses/home_timeline.json" params:param.keyValues success:^(id json) {
            if (success) {
                AyHomeStatusesResult *result = [AyHomeStatusesResult objectWithKeyValues:json];
                [AyStatusCacheTool addStatuses:result.statuses];
                success(result);
            }
        } failure:^(NSError *error) {
            if (failure) {
                failure(error);
            }
        }];

    }

}


+ (void)getStatusDetailWithParam:(AyStatusesDetailParame *)param success:(void (^)(AyStatus *result))success failure:(void (^)(NSError *error))failure{
    [AyHttpTool postWithURL:@"https://api.weibo.com/2/statuses/show.json" params:param.keyValues success:^(id json) {
        if (success) {
            AySendStatusResult *result = [AySendStatusResult objectWithKeyValues:json];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
};


+ (void)sendStatusWithParam:(AySendStatusParam *)param success:(void (^)(AySendStatusResult *))success failure:(void (^)(NSError *))failure{

    [AyHttpTool postWithURL:@"https://api.weibo.com/2/statuses/update.json" params:param.keyValues success:^(id json) {
        if (success) {
            AySendStatusResult *result = [AySendStatusResult objectWithKeyValues:json];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];

}



+ (void)userStatusesWithParam:(AyHomeStatusesParame *)param success:(void (^)(AyHomeStatusesResult *))success failure:(void (^)(NSError *))failure{
    
    [AyHttpTool getWithURL:@"https://api.weibo.com/2/statuses/user_timeline.json" params:param.keyValues success:^(id json) {
        if (success) {
            AyHomeStatusesResult *result = [AyHomeStatusesResult objectWithKeyValues:json];
            //[AyStatusCacheTool addStatuses:result.statuses];
                success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}


+ (void) statusCommentWithParam:(AyStatusCommentParame *) param success:(void (^)(AyStatusCommentResult *result)) success failure:(void (^)(NSError *error)) failure{

    [AyHttpTool getWithURL:@"https://api.weibo.com/2/comments/show.json" params:param.keyValues success:^(id json) {
        if (success) {
            AyStatusCommentResult *result = [AyStatusCommentResult objectWithKeyValues:json];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];

}



+ (void) statusRepostsWithParam:(AyStatusCommentParame *) param success:(void (^)(AyRepostsResult *result)) success failure:(void (^)(NSError *error)) failure{

    [AyHttpTool getWithURL:@"https://api.weibo.com/2/statuses/repost_timeline.json" params:param.keyValues success:^(id json) {
        if (success) {
            AyRepostsResult *result = [AyRepostsResult objectWithKeyValues:json];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];

}

@end
